package fr.sii.android.accessibility.model;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class Geoloc implements LocationListener
{
	
	public LocationManager locationManager;
	public List<MapEntry> listMap;
	public double longitude = 0.0;
	public double latitude = 0.0; 
	
	//CONSTRUCTOR
	public Geoloc(List<MapEntry> list)
	{
		super();
		listMap=new ArrayList<MapEntry>(list);
	}
	
	public void createLocationManager(Context cont)
	{
		//Create the locationManager
		locationManager = (LocationManager) cont.getSystemService(Context.LOCATION_SERVICE);
		
		//RequestUpdates from the locationManager
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
	}
	
	public void closestMaps()
	{
		List<MapEntry>  closestMaps = new ArrayList<MapEntry>();
		
		/*Find the localisation*/
		System.out.print("longi="+longitude);
		System.out.print("lati="+latitude);
		Log.i("loni", "l"+ longitude);
		Log.i("lati", "l"+latitude);	
		
		/*Display the list of Maps*/
		//closestMaps.set(0, listMap.get(0));
		
		for(MapEntry map : listMap)
		{
			Log.i("map", "map: " + map.name + " lat: " + map.lat + " lon: "
				+ map.lon + " url: " + map.url);
			
			//if(latitude-Double.parseDouble(map.lat)+longitude-Double.parseDouble(map.lon)<latitude-Double.parseDouble(map.lat)+longitude-Double.parseDouble(map.lon));
		}
		
		/*Find the closest Maps*/
		
		//return closestMaps;
	}

	@Override
	public void onLocationChanged(Location loc) 
	{
		if (loc != null)
		{
			// TODO Auto-generated method stub
			longitude=loc.getLongitude();
			latitude=loc.getLatitude();
			
			String longitude ="Longitude: " + loc.getLongitude();
			String latitude ="Latitude: " + loc.getLatitude();
			
			Log.i("lon", longitude);
			Log.i("lat", latitude);	
			
			this.closestMaps();
		}
	
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}
